package com.ruoyi.api.job;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.enums.CrmDataStatus;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.CrmJob;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.CJobLog;
import com.ruoyi.system.service.ICJobLogService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 * 
 * @author ruoyi
 * @date 2020-10-14
 */
@RestController
@RequestMapping("/api/crmLog")
public class CJobLogController extends BaseController
{
    @Autowired
    private ICJobLogService cJobLogService;

    /**
     * 根据事件id获取记录列表信息
     * @param jobId
     * @return
     */
    @GetMapping("/getListByJobId")
    public AjaxResult getListByJobId(Long jobId)
    {
        if(jobId == null){
            return AjaxResult.error("任务id 无效");
        }
        CJobLog cJobLog=new CJobLog();
        cJobLog.setcJobId(jobId);
        List<CJobLog> list = cJobLogService.selectCJobLogList(cJobLog);
        return AjaxResult.success(list);
    }

    /**
     * 获取记录详细信息
     */
    @GetMapping(value = "/getJobLogDetail")
    public AjaxResult getInfo(Long id)
    {
        if(id == null){
            return AjaxResult.error("记录id 无效");
        }
        return AjaxResult.success(cJobLogService.selectCJobLogById(id));
    }


    /**
     * 添加事件记录
     * @param crmLog
     * @return
     */
    @PostMapping("/insertCrmLog")
    public AjaxResult insertCrmLog(@RequestBody CJobLog crmLog){
        if(StringUtils.isEmpty(crmLog.getLogContent())){
           return AjaxResult.error("添加失败,无记录内容");
        }
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userId=loginUser.getUser().getUserId();
        crmLog.setCreateUser(userId);
        crmLog.setCreateTime(new Date());
        crmLog.setStatus(CrmDataStatus.OK.getCode());
        return cJobLogService.insertCJobLog(crmLog)> 0 ? AjaxResult.success("添加成功"): AjaxResult.error("添加失败");
    }

    /**
     * 删除事件记录
     */
 	@GetMapping("/deleteCrmLogById")
    public AjaxResult remove(Long id)
    {
        if(id == null){
            return AjaxResult.error("事件记录id无效");
        }
        return toAjax(cJobLogService.deleteCJobLogById(id));
    }
}
